package com.multysite.controller;

import java.io.IOException;
import java.util.logging.Logger;

import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.multysite.entity.admin.ApplicationConfig;
import com.multysite.entity.admin.ApplicationTemplate;
import com.multysite.model.CategoryModel;
import com.multysite.model.NewsModel;
import com.multysite.model.TagModel;
import com.multysite.quercus.CompileScriptEngine;
import com.multysite.util.RecentViewHelper;
import com.multysite.util.Setting;

@SuppressWarnings("serial")
public class HomeServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(HomeServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			if (!NamespaceManager.get().equals(Setting.getGeneralNamespace())) {
				req.setCharacterEncoding("utf-8");
				ApplicationTemplate template = (ApplicationTemplate) req
						.getAttribute("template");
				ApplicationConfig config = (ApplicationConfig) req
						.getAttribute("config");
				if (template != null && config != null) {
					int page = 1;
					try {
						page = Integer.parseInt((String) req
								.getParameter("page"));
					} catch (Exception e) {
						page = 1;
					}
					NewsModel model = new NewsModel();
					model.setPage(page);
					model.prepareList();
					CategoryModel cateModel = new CategoryModel();
					cateModel.prepareAll();
					TagModel tagModel = new TagModel();
					tagModel.prepareList();

					CompiledScript compiledscript = CompileScriptEngine
							.getCompileScript(config.getApplicationId(),
									"home", template.getHome());
					Bindings bind = compiledscript.getEngine().getBindings(
							ScriptContext.ENGINE_SCOPE);
					bind.put("title_for_layout", config.getTitle());
					bind.put("description_for_layout", config.getDescription());
					bind.put("keyword_for_layout", config.getKeyword());
					bind.put("css_for_layout", template.getCss());
					bind.put("js_for_layout", template.getJs());

					bind.put("recent_view", RecentViewHelper.getRecentView());
					bind.put("list_tag", tagModel.getListResult());
					bind.put("list_category", cateModel.getListResult());

					bind.put("current_page", model.getPage());
					bind.put("total_page", model.getTotalPage());
					bind.put("articles", model.getListResult());
					try {
						resp.getWriter().print((String) compiledscript.eval());
					} catch (Exception e) {
						e.printStackTrace();
						log.warning(e.toString());
					}
				} else {
					log.warning("Have no namespace");
					resp.getWriter().println("Error ! Please try again later");
				}
			} else {
				NamespaceManager.set(Setting.getGeneralNamespace());
				req.getRequestDispatcher("/admin/pages/home.jsp").forward(req,
						resp);
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.warning(e.toString());
		}
	}
}
